<?php
Namespace Cyfy\Modules\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms\Module;

/**
 * CheckBox class.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class CheckBox extends Input
{
    /**
     * Whether the input is checked.
     * @var bool
     */
    public $checked = false;

    /**
     * Creates a new element checkbox.
     *
     * @param string Checkbox name
     * @param string Checkbox title
     * @param string Default value
     * @param bool Required to check
     * @param bool Is checked
     */
    function __construct($name, $title, $value = '', $required = false, $checked = false)
    {
        $this -> name = $name;
        $this -> title = $title;
        $this -> value = $value;
        $this -> required = $required;
        $this -> checked = $checked;
    }
    
    /**
     * Returns a string containing the checkbox.
     * @return string HTML content
     */
    function __toString()
    {
        $this -> loadDescription();
        $required = ($this->required) ? " form-validate" : "";
        $star = ($this->required) ? Module :: REQUIRED_CHAR : "";
        $checked = ($this->checked) ? "checked=\"checked\"" : "";
        $output = "<div class=\"form-item form-item-{$this->name}\"> \n";
        $output .= "    <label class=\"form-label-{$this->name}{$required}\" title=\"{$this->description}\" for=\"{$this->name}\">".$this -> title.": {$star}</label> \n";
        $output .= "	<input type=\"checkbox\" class=\"form-input-{$this->name}\" name=\"{$this->name}\" value=\"{$this->value}\" {$checked}/>";
        $output .= "</div> \n\n";
        return $output;
    }
    
    /** 
     * Validates the data.
     * @return bool
     */
    function validate()
    {
        // is required
        if($this->required){
            if($this -> value == ""){
                Message :: set(Module :: t(111, array($this -> title)), Message::ERROR);
                return false;
            }
        }
        return true;
        
    }

    
}
    